package com.yn.club.auth.application.controller;



import com.yn.club.auth.domain.entity.AuthRoleDO;
import com.yn.club.auth.application.dto.AuthRoleDTO;
import com.yn.club.auth.domain.service.AuthRoleDomainService;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.List;
import com.yn.club.common.util.R;
import com.yn.club.common.util.PageReq;
import com.yn.club.common.util.PageResp;
import com.yn.club.auth.application.convert.AuthRoleApplicationConverter;
import com.yn.club.auth.application.request.AuthRoleQueryRequest;
import com.yn.club.auth.application.request.AuthRoleSaveRequest;

/**
 * (AuthRole)表控制层
 *
 * @author YN
 * @since 2025-01-22 15:41:49
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("role")
public class AuthRoleController {
    /**
     * 服务对象
     */
    private final AuthRoleDomainService authRoleDomainService;
    private final AuthRoleApplicationConverter authRoleApplicationConverter;

    /**
     * 分页查询数据
     *
     * @param pageReq 分页参数
     * @param query   查询条件
     * @return 所有数据
     */
    @GetMapping("/page")
    public R<PageResp<AuthRoleDTO>> queryPage(PageReq pageReq, AuthRoleQueryRequest query) {
        AuthRoleDO authRoleDO = authRoleApplicationConverter.convertQueryRequestToDo(query);
        PageResp<AuthRoleDO> page = authRoleDomainService.page(pageReq, authRoleDO);
        
        List<AuthRoleDTO> authRoleDTOS = authRoleApplicationConverter.convertDoToDto(page.getList());
        return R.ok(PageResp.of(page,authRoleDTOS));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R<AuthRoleDTO> selectOne(@PathVariable Serializable id) {
    AuthRoleDTO authRoleDTO = authRoleApplicationConverter.convertDoToDto(
      this.authRoleDomainService.getById(id));
     
    return R.ok(authRoleDTO);
    }

    /**
     * 新增数据
     *
     * @param authRole 实体对象
     * @return 受影响条数
     */
    @PostMapping("/insert")
    public R<Boolean> insert(@RequestBody @Validated AuthRoleSaveRequest authRole) {
       AuthRoleDO authRoleDO = authRoleApplicationConverter.convertSaveRequestToDo(authRole);
        return R.ok(this.authRoleDomainService.insert(authRoleDO));
    }

    /**
     * 修改数据
     *
     * @param authRole 实体对象
     * @return 受影响条数
     */
    @PutMapping("/update")
    public R<Boolean> update(@RequestBody AuthRoleSaveRequest authRole) {
        AuthRoleDO authRoleDO = authRoleApplicationConverter.convertSaveRequestToDo(authRole);
        return R.ok(this.authRoleDomainService.updateById(authRoleDO));
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 受影响条数
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestBody @NotEmpty List<Integer> ids) {
        return R.ok(this.authRoleDomainService.delete(ids));
    }
}

